﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.Commands;

namespace Mimoza.Apps.Console.ViewModel
{
    public class ContainerTreeViewModel : BaseViewModel
    {
        private Dictionary<ContainerType, BaseViewModel> _containerModels = new Dictionary<ContainerType, BaseViewModel>();

        public override void Init(Mimoza.Apps.Console.Application application)
        {
            base.Init(application);

            m_application.ObjectLayer.ContainerRepository.OnObjectChanged += new EventHandler<ObjectLayer_ObjectChangedEventArgs>(ContainerRepository_OnObjectChanged);
            this.Context = m_application.ObjectLayer.ContainerRepository.RootContainer;

            m_ToolBarCommands.Add(new NewContainerCommand(this));
            m_ToolBarCommands.Add(new AddPolicyCommand(this));
            m_ToolBarCommands.Add(new DeleteContainerCommand(this));

            m_ContextCommands.Add(new NewContainerCommand(this));
            m_ContextCommands.Add(new DeleteContainerCommand(this));
            m_ContextCommands.Add(new RenameContainerCommand(this));
            m_ContextCommands.Add(null);
            m_ContextCommands.Add(new AddPolicyCommand(this));
        }

        public override Object Context
        {
            get { return base.Context; }
            set
            {
                base.Context = value;

                ActiveContainerViewModel();
            }
        }

        private void ActiveContainerViewModel()
        {
            Mimoza.ObjectLayer.Container cont = base.Context as
                Mimoza.ObjectLayer.Container;

            foreach (var model in _containerModels.Values)
            {
                model.Visible = false;
            }

            if (cont != null && _containerModels.ContainsKey(cont.Type))
            {
                //_containerModels[cont.Type].OnActivate();
                _containerModels[cont.Type].Visible = true;
            }
        }

        void ContainerRepository_OnObjectChanged(object sender, ObjectLayer_ObjectChangedEventArgs e)
        {
            RaiseOnObjectChanged(e);
        }

        public void RegisterContainerTypeViewModel(ContainerType type, BaseViewModel viewModel)
        {
            _containerModels[type] = viewModel;
        }
       
        public IEnumerable<Mimoza.ObjectLayer.Container> GetChildsContainers(Mimoza.ObjectLayer.Container root)
        {
            if ( root == null )
            {
                yield return this.m_application.ObjectLayer.ContainerRepository.RootContainer; 
            } else {                
                foreach ( Mimoza.ObjectLayer.Container cont in root.GetChilds())
                {
                    yield return cont;
                }
            }
        }        
     }
}
